package org.apache.struts.ehr.medical_records.action;

import java.util.Map;

import org.apache.axis2.AxisFault;
import org.apache.struts.ehr.medical_records.model.HL7Message;
import org.apache.struts.ehr.medical_records.model.Query;
import org.apache.struts.ehr.medical_records.model.ReplyMessage;
import org.apache.struts.ehr.medical_records.model.TransferToAnotherHospital;
import org.apache.struts.ehr.medical_records.model.Ward;

import com.earogya.Constants;
import com.earogya.communicate.Communicator;
import com.earogya.logic.medical_records.CreateADTA02;
import com.earogya.logic.medical_records.CreateQRYA19;
import com.earogya.logic.medical_records.ParseReply;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/***
 * This class is the struts action to transfer patient to
 * another hospital
 * @author Chamendri
 *
 */
public class TransferToAnotherHospitalAction extends ActionSupport{

	private TransferToAnotherHospital transferToAnotherHospital;
	private HL7Message message;
	private ReplyMessage replyMessage;
	private Ward ward;
	private String occupiedBeds;
	private Boolean isReply=false;
	private Boolean isWardEmpty=false;
	
	public Boolean getIsWardEmpty() {
		return isWardEmpty;
	}

	public Boolean getIsReply() {
		return isReply;
	}


	public String getOccupiedBeds() {
		return occupiedBeds;
	}

	public Ward getWard() {
		return ward;
	}

	public void setWard(Ward ward) {
		this.ward = ward;
	}
	
	public ReplyMessage getReplyMessage() {
		return replyMessage;
	}

	public HL7Message getMessage() {
		return message;
	}

	public TransferToAnotherHospital getTransferToAnotherHospital() {
		return transferToAnotherHospital;
	}

	public void setTransferToAnotherHospital(TransferToAnotherHospital transferToAnotherHospital) {
		this.transferToAnotherHospital = transferToAnotherHospital;
	}
	
	/**
	 * overrides the execute method in ActionSupport interface
	 * creates ADT A02 message with the parameter with "hospital".
	 * returns ERROR or SUCCESS.
	 */
	public String execute() throws Exception {
		Map<String, Object> session=ActionContext.getContext().getSession();
		replyMessage=new ReplyMessage();
		replyMessage.setMessage(Constants.EMPTY);
		Query query=new Query();
		CreateADTA02 createMessage=new CreateADTA02(transferToAnotherHospital);
		String msg=createMessage.createMessage(Constants.SENDING_FACILITY, Constants.TRANSFER_HOSPITAL);
		message=new HL7Message();
		if(!msg.equals(null)){
			Communicator c=new Communicator();
			String reply=c.sendMessage(msg);
			if(!reply.equals(Constants.NO_REPLY)){
				ParseReply parse=new ParseReply();
				replyMessage=(ReplyMessage)parse.parseMessage(reply);
				if(replyMessage.getMessage().equals(Constants.SUCCESS)){
					Query query1=new Query();
					query1.setPatientId(transferToAnotherHospital.getFromWardNo());
					query1.setSearch_type(Constants.OCCUPIED_BEDS);
					CreateQRYA19 qry=new CreateQRYA19(query1);
					String msg1=qry.createMessage(Constants.SENDING_FACILITY);
					if(!msg1.equals(null)){
						Communicator c1=new Communicator();
						try {
							String reply1=c1.sendMessage(msg1);
							if(!reply.equals(Constants.NO_REPLY)){
								ParseReply parse1=new ParseReply();
								occupiedBeds=(String)parse1.parseMessage(reply1);
								if(occupiedBeds!=null){
									isReply=true;
								}else{
									isWardEmpty=true;
									isReply=true;
								}
								ward=new Ward();
								ward.setWardNo(Integer.parseInt(transferToAnotherHospital.getFromWardNo()));
							}else{
								replyMessage.setMessage(reply1);
							}
						} catch (AxisFault e) {
							replyMessage.setMessage(e.getReason());
							return ERROR;
						} catch (Exception e) {
							replyMessage.setMessage(e.getMessage());
							return ERROR;
						}
					}else{
						replyMessage.setMessage("Failed to create request message");
					}
				}else{
					return ERROR;
				}
			}
			else{
				replyMessage.setMessage(reply);
				return ERROR;
			}
		}
		else{
			replyMessage.setMessage("Failed to create request message");
		}
		replyMessage.setMessage("Patient id "+transferToAnotherHospital.getPatientId()+" is successfully transfered to "+transferToAnotherHospital.getToHospital());
		return SUCCESS;
	}
	
	/**
	 * Overrides the validate method in ActionSupport interface.
	 * Validates the input fields in transfer to another hospital form.
	 */
	public void validate(){
		if(transferToAnotherHospital.getPatientId()==0){
			addFieldError("transferToAnotherHospital.patientId", "This field is required");
		}
		if(transferToAnotherHospital.getFromBedNo().length()==0){
			addFieldError("transferToAnotherHospital.fromBedNo", "This field is required");
		}
		if(transferToAnotherHospital.getFromHospital().length()==0){
			addFieldError("transferToAnotherHospital.fromHospital", "This field is required");
		}
		if(transferToAnotherHospital.getFromWardNo().length()==0){
			addFieldError("transferToAnotherHospital.fromWardNo", "This field is required");
		}
		if(transferToAnotherHospital.getReason().length()==0){
			addFieldError("transferToAnotherHospital.reason", "This field is required");
		}
		if(transferToAnotherHospital.getToHospital().length()==0){
			addFieldError("transferToAnotherHospital.toHospital", "This field is required");
		}
		if(transferToAnotherHospital.getFromHospital().length()!=0 && transferToAnotherHospital.getToHospital().length()!=0){
			if(transferToAnotherHospital.getFromHospital().equalsIgnoreCase(transferToAnotherHospital.getToHospital())){
				addFieldError("transferToAnotherHospital.toHospital", "Destination is as same as the current hospital");
			}
		}
		if(transferToAnotherHospital.getTransferringOfficerId().length()==0){
			addFieldError("transferToAnotherHospital.transferringOfficerId", "This field is required");
		}
		if(transferToAnotherHospital.getTransferTime()==null){
			addFieldError("transferToAnotherHospital.transferTime", "This field is required");
		}
	}
}
